// src/store/Modules.ts

export default {
    auth: {
        namespaced: true,
        state: {
            username: "",
            userId: "",
            token: "",
            avatar:"",
            isLogin:false,
            tabbar:"index",
            uToast:null,
        },
        mutations: {
            setToken(_state: any, token: string) {
                if (token) {
                    _state.token = token
                } else {
                    _state.token = ""
                }
            },
            setUsername(_state: any, username: string) {
                if (username) {
                    _state.username = username
                } else {
                    _state.username = ""
                }
            },
            setUserId(_state: any, userId: string) {
                if (userId) {
                    _state.userId = userId
                } else {
                    _state.userId = ""
                }
            },
            setIsLogin(_state: any, isLogin: boolean) {
                _state.isLogin = isLogin
            },
            setUToast(_state: any, uToast: any) {
                _state.uToast = uToast
            },
            setTabbar(_state: any, tabbar: any) {
                _state.tabbar = tabbar
            },
            setAvatar(_state: any, avatar: any) {
                if (avatar) {
                    _state.avatar = avatar
                } else {
                    _state.avatar = ""
                }
            },
        },
        actions: {
            setToken(context: any, token: string) {
                context.commit('setToken', token)
            },
            setUsername(context: any, username: string) {
                context.commit('setUsername', username)
            },
            setUserId(context: any, userId: string) {
                context.commit('setUserId', userId)
            },
            setIsLogin(context: any, isLogin: boolean) {
                context.commit('setIsLogin', isLogin)
            },
            setUToast(context: any, uToast:any) {
                context.commit('setUToast', uToast)
            },
            setTabbar(context: any, tabbar: any) {
                context.commit('setTabbar', tabbar)
            },
            setAvatar(context: any, avatar: any) {
                context.commit('setAvatar', avatar)
            },
            logout(context: any, callback: (goLogin: ()=>void)=>void) {
                context.commit('setToken')
                context.commit('setUsername')
                context.commit('setUserId')
                context.commit('setAvatar')
                context.commit('setIsLogin',false)
                callback(():void=>{
                    uni.navigateTo({
                        url: '/pages/login/login'
                    })
                })

            },
        },
        getters: {
            getToken(_state: any, _getters: any): string {
                return _state.token
            },
            getUsername(_state: any, _getters: any): string {
                return _state.username
            },
            getUserId(_state: any, _getters: any): string {
                return _state.userId
            },
            getIsLogin(_state: any, _getters: any): string {
                return _state.isLogin
            },
            getUToast(_state: any, _getters: any): string {
                return _state.uToast
            },
            getTabbar(_state: any, _getters: any): string {
                return _state.tabbar
            },
            getAvatar(_state: any, _getters: any): string {
                return _state.avatar
            }
        }
    }
}